class AdController < ApplicationController
  before_filter :login_required, :only=>['bid','buy']
  before_filter :spam_required, :only=>['contact','report']
  caches_page :view, :ad_info, :info_box
  
  def view
    @ad = Ad.find(params[:id])
    @page_title = @ad.title
    begin
      @max_bid = @ad.auction.winner
      @bid_history = Bid.history(@ad.auction)
      logger.debug("max_bid user: #{@max_bid.user.username}")
    rescue
      #not an auction
    end

    @user_is_subscriber = AdSubscription.is_subscriber(@ad.id,session[:user].id) if session[:user]
  end

  def timeleft
    @ad = Ad.find(params[:id])
    render :partial => "timeleft"
  end

  def view_minimal
    view
    render :action => :view
  end

  def subscription
    @ad = Ad.find(params[:id])
    @user_is_subscriber = AdSubscription.is_subscriber(@ad.id,session[:user].id) if session[:user]
    render :action => :subscription
  end

  def ad_info
    @ad = Ad.find(params[:id])
    render :action => :ad_info
  end

  def preview
    view
    render :action => :view
  end
  
  def bid
    auction = Auction.find(params[:auction])
    if Time.now < auction.ad.enddate && params[:bid].to_i > 0
      Bid.make_bid(session[:user].id,params[:bid],params[:auction])
      redirect_to :action => :view, :id => Auction.find(params[:auction]).ad.id
    end
  end

  def bids
    bid_history = Bid.history(params[:id])
    render :partial => "ad/bids", :locals => { :bid_history => bid_history }
  end

  def buy
    ad = Ad.find(params[:id])
    if Time.now < ad.enddate
      ad.buy(session[:user])
      redirect_to :action => :view, :id => ad.id
    end
  end

  def remove
    ad = Ad.find(params[:id])
    if (session[:user].id == ad.user_id)
      ad.remove
      redirect_to :controller => :private, :action => :sales
    else
      render :text => "Inte tillåtet, du äger inte annonsen."
    end
    Email.notifyAdRemoved(ad)
  end

  def report
    #params.each { |k,v| logger.debug("#{k} = #{v}") }
    load_params_from_session
    #params.each { |k,v| logger.debug("#{k} = #{v}") }
    
    ad_id = params[:id]
    message = params[:message]

    Report.create(:ad_id => ad_id, :message => message)
    
    render :text => "Tack för hjälpen!"
  end

  def contact
    #params.each { |k,v| logger.debug("#{k} = #{v}") }
    load_params_from_session
    #params.each { |k,v| logger.debug("#{k} = #{v}") }
    
    @ad = Ad.find(params[:id])
    @admessage = AdMessage.new
    @admessage.email = params[:admessage][:email]
    @admessage.tel = params[:admessage][:tel]
    @admessage.message = params[:admessage][:message]

    if (@admessage.valid?)
      if (Email.sendAdMessage(@admessage,@ad))
        render :text => "Ditt meddelande har nu skickats till säljaren."
      else
        render :text => "Ditt meddelande kunde, av okänd anledning, inte skickas.<br/>#{@message.message}"
      end
    else
      render :partial => "contact_form", :locals => { :ad => Ad.find(params[:id]) }
    end
  end

  def contact_form
    @admessage = AdMessage.new
    @admessage.email = (session[:user] ? session[:user].email : "@")
    @admessage.tel = (session[:user] ? session[:user].tel : "")
    @ad = Ad.find(params[:id])
    render :action => :contact_form
  end

  def toggle_subscription
    ad_id = params[:id]
    user_id = session[:user].id
    AdSubscription.toggle_subscription(ad_id,user_id)
    user_is_subscriber = AdSubscription.is_subscriber(ad_id,user_id)
    render :partial => "subscription", :locals => { :user_is_subscriber => user_is_subscriber, :ad_id => ad_id }
  end

  def report_display
    ad_id = params[:id]
    render :partial => "report_form", :locals => { :ad_id => ad_id }
  end

  def infobox
    render :partial => "infobox_ad", :locals => { :ad => Ad.find(params[:id]) }
  end
end
